﻿require.config({
	urlArgs : "version=20170407",
	paths : {
		'jquery' : '/js/packages/jquery-2.14-min',
		'bootstrap' : '/bootstrap/js/bootstrap.min',
		'bootstraptree' : '/bootstrap/bootstrap-treeview/bootstrap-treeview',
	},
	shim : {
		'bootstrap' : {
			deps : [ 'jquery' ],
			exports : 'bootstrap'
		},
		'bootstraptree' : {
			deps : [ 'jquery' ],
			exports : 'bootstrap'
		}
	}

});


require([ 'jquery', 'bootstrap', 'bootstraptree' ], function($) {
	//Select Depot
	var curr_page = 1;
	var curr_size = 20;
	var total_page;
	loadpage(curr_page,curr_size);

	
	function loadpage(pageNumber,pageSize) {
		console.log("load result");
		//添加通信机制、更新检索信息		
		
		$.ajax({
			url:'/sptree/all',
		    type:'GET', //GET
		    async:false,    //或false,是否异步
		    data:{
		    	pageNumber:pageNumber,
		    	pageSize:pageSize,
		    	//perSize:perSize
		    },
		    //timeout:5000,    //超时时间
		    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
		    /*beforeSend:function(xhr){
		        console.log(xhr)
		        console.log('发送前')
		    }*/
		    success:function(data,textStatus,jqXHR){
		    	//if(data==undefined) console.log('no data')
		    	if(data.length==0){
		    		console.log('not found')
		    		$('#no-result-div').prop('style',"display:block")
		    	}else{
		    		$('#search-result-div').prop('style',"display:block")
		    		showSearch(data,1,20);
		    		show_pages(data.page,data.totalPages);
		    	}
		    	
		        //console.log(data)
		        //console.log(textStatus)
		        //console.log(jqXHR)
		    },
		    error:function(xhr,textStatus){
		        //console.log('什么错误')
		        //console.log(xhr.responseText)
		        //console.log(textStatus)
		    },
		    complete:function(){
		        //console.log('结束')
		    }
		});
	}
	
	function showSearch(data,page,pageSize) {
		$('.chooseall').prop("checked", false);
		$('#table-all')[0].innerHTML="";
		for(var o in data){
			//console.log(data[o].id)
			var dataObj=data[o].patent;//eval("("+data[o]+")");
			var order = 1+parseInt(o)+(parseInt(page)-1)*parseInt(pageSize)
			//console.log(data)
			addtd(order,data[o].id,data[o],'table-all');
			//addtd(parseInt(data[o].id),dataObj,parseInt(data[o].score),'show-table');
		}
		//showSelected();
	}
	function addtd(order,o,dataObj,idid){
		var tr = document.createElement("tr");
		tr.setAttribute('id','tr-'+o);
		
		var div_start = "<div class = 'table-content'>";

		var div_abs = document.createElement("div");
		div_abs.setAttribute("class","table-content");
		div_abs.setAttribute("id","abs-"+o);
		
		var div_right = document.createElement("div");
		div_right.setAttribute("class","table-content");
		div_right.setAttribute("id","right-"+o);
		
		var div_end = "</div>";
		
		var td1 = document.createElement("td");
		td1.setAttribute("class","show-column1");
		td1.innerHTML=order;
		var td2 = document.createElement("td");
		td2.setAttribute("class","show-column1");
		td2.innerHTML=div_start+dataObj.name+div_end;
		var td3 = document.createElement("td");
		td3.setAttribute("class","show-column1");


		tr.appendChild(td1);
		tr.appendChild(td2);
		tr.appendChild(td3);
		
		$('#'+idid).append(tr);
		//
	}
	
	
	function initKeywordsTreesSet() {
		$.ajax({
			url : '/sptree/all',
			type : 'GET',
			async : false,
			dataType : 'json',
			success : function(data, textStatus, jqXHR) {
				//console.log(data)
				if(data==null||data.length==0){
					$('#no-result-div').css('display','block')
				}else{
					$('#tree-result-div').css('display','block')
					showSearch(data,page,pageSize)
				}
			}
		});
	}
		function GetAndShowTree(treeId){
			$.ajax({
				url : '/sptree/readflow',
				type : 'GET', //GET
				async : false, //或false,是否异步
				data : {
					treeId : treeId,
				},
				dataType : 'json', //返回的数据格式：json/xml/html/script/jsonp/text
				success : function(data, textStatus, jqXHR) {
					formatData(data)
					//console.log(data)
					//console.log(textStatus)
					//console.log(jqXHR)
				},
				error : function(xhr, textStatus) {
					console.log(xhr.responseText)
					console.log(textStatus)
				},
				complete : function() {
					//console.log('结束')
				}
			});
			var checkText=$("#show-trees").find("option:selected").text();  //获取Select选择的Text
			var checkValue=$("#show-trees").val(); 
			//console.log(checkText+checkValue)
			$('#update-tree-name-input').val(checkText)
			initSelectableTree();
		}

		$('#show-trees').change(function() {
			console.log($(this).children('option:selected').val());
			console.log($(this).children('option:selected').text());
			
			//console.log(depotid)
			newdepotid = $(this).children('option:selected').val();
			if(depotid == newdepotid){
				//console.log('nothing')
			}
			else{
				depotid = newdepotid
				//console.log('new depot id: '+ depotid)
				GetAndShowTree(depotid)
			}
		});
		
		$("#create-new-tree").click(
				function (){
					if($("#new-tree-name").val().trim()==''){
						alert('请输入关键词分类树名称');
						return;
					}
					
					$.ajax({
						url : '/sptree/newsptree',
						type : 'GET', //GET
						async : false, //或false,是否异步
						data : {
							treeName : $("#new-tree-name").val().trim(),
						},
						dataType : 'json', //返回的数据格式：json/xml/html/script/jsonp/text
						success : function(data, textStatus, jqXHR) {
							$('#show-trees').append(
									'<option value="' + data + '">'
											+ $("#new-tree-name").val().trim() + '</option>');
							$("#show-trees").val(data);
							$("#show-trees").trigger('change')
							
							$('#myModal').modal('hide')
							$("#new-tree-name").val('')
							
							$('#no-result-div').prop('style','display:none')
							$('#tree-result-div').prop('style','display:block')
						}
					});
					//initSelectableTree();
			});
		
		$("#update-tree-name").click(function (){
			if($('#update-tree-name-input').val()==null|| $('#update-tree-name-input').val()==''){
				alert('请输入关键词分类树名称')
			}
			
			$.ajax({
				url : '/sptree/renamesptree',
				type : 'GET', //GET
				async : false, //或false,是否异步
				data : {
					treeId :$('#show-trees').children('option:selected').val(),
					treeName :  $('#update-tree-name-input').val()
				},
				dataType : 'text', //返回的数据格式：json/xml/html/script/jsonp/text
				success : function(data, textStatus, jqXHR) {
					newtext = $('#update-tree-name-input').val();
					$('#show-trees').children('option:selected').text(newtext)
				},
			});

		});

	});